//! \file GuiSDL.cpp
//! \brief TODO: Document
// -----------------------------------------------------------------------------
//! This program is free software; you can redistribute it and/or
//! modify it under the terms of the GNU General Public License
//! as published by the Free Software Foundation; either version 2
//! of the License, or (at your option) any later version.
//!
//! This program is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with this program; if not, write to the Free Software Foundation,
//! Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// -----------------------------------------------------------------------------

#include "GuiSDL.h"

// =====[ Local Definitions ]===================================================

// =====[ Local DataTypes ]=====================================================

// =====[ Local Variables ]=====================================================
GuiSDL* GuiSDL::m_pGuiSDL = NULL;         //!< The singleton object instance.
SDL_Surface *GuiSDL::m_pSurface = NULL;   //!< TODO: document

// =====[ Local Functions ]=====================================================

// =====[ Private (Internal) Methods ]==========================================

// =====[ Local Implementation ]================================================


// =============================================================================
// GuiSDL::getSingleton
// -----------------------------------------------------------------------------
//! Returns a reference to the instanced singleton object.
//!
//! \retval Singleton
//!   A reference to the instanced singleton object.
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
GuiSDL& GuiSDL::getSingleton()
{
  // Only allow one instance of class to be generated.
  if (!m_pGuiSDL) { m_pGuiSDL = new GuiSDL; }   // Create our single instance.
  return (*m_pGuiSDL);                          // Finally, return our singleton.
}


// =============================================================================
// GuiSDL::getSingletonPtr
// -----------------------------------------------------------------------------
//! Returns a pointer to the instanced singleton object.
//!
//! \retval Singleton
//!   A pointer to the instanced singleton object.
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
GuiSDL* GuiSDL::getSingletonPtr()
{
  // Only allow one instance of class to be generated.
  if (!m_pGuiSDL) { m_pGuiSDL = new GuiSDL; }   // Create our single instance.
  return (m_pGuiSDL);                           // Finally, return our singleton.
}


// =============================================================================
// GuiSDL::start
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
tERROR GuiSDL::start(void)
{
  // Initialize SDL.
  if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    { return (stop(ERR_GUI_DISPLAY_START)); }

  // Create our SDL drawing surface.
  m_pSurface = SDL_SetVideoMode(800, 600, 0, SDL_OPENGL);
  if (m_pSurface == NULL)
    { return (stop(ERR_SDL_SURFACE_CREATION)); }

  // Hide the SDL cursor.
  SDL_ShowCursor(SDL_DISABLE);

  // Activate unicode translation for SDL key events.
  SDL_EnableUNICODE(1);

  // Enable the key repeats.
  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

  // Return success.
  return (ERR_NONE);
}


// =============================================================================
// GuiSDL::stop
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
tERROR GuiSDL::stop(tERROR Error)
{
  // Free the drawing surface.
  if (m_pSurface != NULL) { SDL_FreeSurface(m_pSurface); }

  // Return the original error code.
  return (Error);
}


// =============================================================================
// FxName
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
void GuiSDL::swapBuffers(void)
{
  // Update the screen with a page flip.
  SDL_GL_SwapBuffers();
}

